package com.xintonghua.bussiness.ui.user;

import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.GroupLogAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.GroupLogBean;
import com.xintonghua.bussiness.bean.GroupLogResponse;
import com.xintonghua.bussiness.event.LogRefreshEventBus;
import com.xintonghua.bussiness.ui.user.log.AddLogActivity;
import com.xintonghua.bussiness.util.MyState;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/3/5.
 * 包名：com.xintonghua.bussiness.ui.user
 */

public class GroupLogActivity extends BaseActivity implements XRecyclerView.LoadingListener {
    @BindView(R.id.lv_log)
    XRecyclerView lvLog;
    GroupLogAdapter adapter;
    private List<GroupLogBean> list = new ArrayList<>();
    private int page = 1;
    GroupLogResponse response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_log);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("成长日志", "添加", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(AddLogActivity.class);
            }
        });


        RefreshUtils.initList(this, lvLog, MyUtils.dip2px(this, 4), R.color.grey_bg);
        adapter = new GroupLogAdapter(this, list);
        lvLog.setAdapter(adapter);
        lvLog.setLoadingListener(this);
        find();
    }

    /**
     * 加载数据
     */
    private void find() {
        httpCent.growthLog(page, this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                if (page == 1) {
                    list.clear();
                }
                response = JSONObject.parseObject((String) data, GroupLogResponse.class);
                list.addAll(response.getList());
                adapter.notifyDataSetChanged();
                RefreshUtils.onRefresh(adapter, lvLog);
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        find();

    }

    @Override
    public void onLoadMore() {
        if (RefreshUtils.hasNext(response,adapter,lvLog)){
            page++;
            find();
        }

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LogRefreshEventBus eventBus){
        page=1;
        find();
    }
}
